package xin.nick.rpc.provider.server;

import io.netty.util.concurrent.DefaultThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @author Nick
 * @date 2021/7/4
 * @description
 */
@Component
public class RpcServerStarter implements CommandLineRunner {

    @Autowired
    private RpcServer rpcServer;

    @Autowired
    private ZkProviderRegister zkProviderRegister;

    @Value("${rpc.port}")
    private int port;

    @Value("${rpc.ip}")
    private String ip;

    @Override
    public void run(String... args) throws Exception {
        ThreadFactory namedThreadFactory = new DefaultThreadFactory("server-pool");
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        singleThreadPool.execute(()-> rpcServer.startServer("127.0.0.1", port));

        // 注册中心注册好
        zkProviderRegister.start();

        singleThreadPool.shutdown();
    }
}
